<?

/** Debugging functions.
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *  
 *  @author Henri Ranki 14.9.2000
 *
 *  General debug functions. Used to debug code. Marvellous.
 */

/* Debug levels. The higher (the smaller number) the less messages/errors
 * are produced/recorded. Lower level messages include all the messages from
 * the levels above.
 * SOD_NONE no debugging messages produced - not recomended
 * SOD_SKY_E very high level errors are recorded
 * SOD_SKY all very high level messages are recorded
 * SOD_HI_E high level errors are recorded
 * SOD_HI all high level messages are recorded
 * SOD_LO_E low level error are recorded
 * SOD_LO all low level messages are recorded
 * SOD_VAL_E XXXX Henkka, is this in use, can it be removed?
 * SOD_FLOW  XXXX --"--
 * SOD_DET Detailed debugging level messages are recorded
 * SOD_DET Extra detailed debugging level messages are recorded. Produces
 *         tons to debugging messages and raw HTML output sent to browser.
 *         Use only when you truly need to see the raw HTML.
 */
define(SOD_NONE, 0);
define(SOD_SKY_E, 1);
define(SOD_SKY, 2);
define(SOD_HI_E, 3);
define(SOD_HI, 4);
define(SOD_LO_E, 5);
define(SOD_LO, 6);
define(SOD_VAL_E, 7);
define(SOD_FLOW, 10);
define(SOD_DET, 12);
define(SOD_EDET, 13);


include_once("sostring.php");

/* Set default values. Developper can change these values later.
   NOTE: that these values must be set after the include clause! */
$soDebugLevel = 0;
$soDebugFilter = "";
$soDebugLogFile = "";
$soFormatDebugHtml = false; // The most stupid name for soDebugFormatHtml
$soDebugHtmlFormat = 0; // Added 6.8.03 to replace the use of $soFormatDebugHtml



/** Prints a debugging message to stdout of file.
 *  Produces a debug message, if specified debug level is less or equal to
 *  global $soDebugLevel. Target, to print the message into, is specified
 *  global $soDebugLogFile that can be left empty (==stdout). Also, global
 *  $soDebugFilter can be set to limit the messages to those including this
 *  string as a sub-string.
 *  @param int $Level Debugging level. When global $soFormatDebugHtml is set,
 *  all error messages are formatted using HTML tags.
 *  @param string $Message Debugging message.
 *  @param int $Level Debugging level.
 */
function soDebug($Message, $Level = 1) {
    global $soDebugLevel, $soFormatDebugHtml, $soDebugHtmlFormat, 
           $soDebugLogFile, $soDebugFilter;

    if (($Level > $soDebugLevel) ||
        (($soDebugFilter != "") &&
         (soLocateStr($Message, $soDebugFilter, 1) == -1))) return; // <-- EXIT

    $Message = date("d.m.y h:i:s")." ".$Level."/".$soDebugLevel.">> ".$Message;
    if ($soFormatDebugHtml || $soDebugHtmlFormat) $Message = "<P>".$Message."</P>";
    
    if ($soDebugLogFile) {
        $fp = fopen($soDebugLogFile, "a");
        fputs($fp,  "$Message\n");
        fclose($fp);
    } else {
        echo "\n$Message\n";
        flush();
    };
} // soDebug

?>
